package com.gamenew.bluetoothdemo.ui.activity;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;

import com.gamenew.bluetoothdemo.R;
import com.gamenew.bluetoothdemo.adapter.BluetoothListAdapter;
import com.gamenew.bluetoothdemo.bean.BluetoothBean;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_ENABLE_BT = 0x1;
    private Button mBtnScaning;
    private ImageView img_loading;
    private Animation operatingAnim;
    private List<BluetoothBean> mList = new ArrayList<>();

    private Handler mHandler = new Handler();

    private ListView mLv;

    private BluetoothAdapter bluetoothAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();

        //先检查设备是否支持蓝牙，再开启蓝牙
        if (getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_BLUETOOTH_LE)) {
            BluetoothManager manager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
            bluetoothAdapter = manager.getAdapter();
            if (bluetoothAdapter != null || !bluetoothAdapter.isEnabled()) {
                Intent enabledIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enabledIntent, REQUEST_ENABLE_BT);
            }
        }
    }

    private void initData() {
        BluetoothBean bean1 = new BluetoothBean();
        bean1.setMac("AC:37:43:F6:98:DF");
        bean1.setName("冷敷器1");
        bean1.setRssi(-91);

        BluetoothBean bean2 = new BluetoothBean();
        bean2.setMac("FC:F1:36:5C:44:51");
        bean2.setName("冷敷器2");
        bean2.setRssi(-93);

        mList.add(bean1);
        mList.add(bean2);
    }

    private void initViews() {
        mLv = (ListView) findViewById(R.id.list_device);

        img_loading = (ImageView) findViewById(R.id.img_loading);
        operatingAnim = AnimationUtils.loadAnimation(this, R.anim.rotate);
        operatingAnim.setInterpolator(new LinearInterpolator());

        mBtnScaning = (Button) findViewById(R.id.btn_start);

        final BluetoothListAdapter adapter = new BluetoothListAdapter(this,mList);
        mLv.setAdapter(adapter);

        mBtnScaning.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mList.clear();
                img_loading.startAnimation(operatingAnim);
                new Thread(){
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                img_loading.clearAnimation();
                                initData();
                                adapter.notifyDataSetChanged();
                            }
                        });
                    }
                }.start();
            }
        });

        mLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                IndexActivity.actionActivity(MainActivity.this);
                finish();
            }
        });
    }

    public static void actionActivity(Context context){
        Intent intent = new Intent(context,MainActivity.class);
        context.startActivity(intent);
    }


}
